import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './user/user.module';
import { User } from './user/entities/user.entity';
import { Role } from './user/entities/role.entity';
import { Permission } from './user/entities/permission.entity';
import { RedisModule } from './redis/redis.module';
import { EmailModule } from './email/email.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { LoginGuard } from './login.guard';
import { PermissionGuard } from './permission.guard';
import { MeetingRoomModule } from './meeting-room/meeting-room.module';
import { MeetingRoom } from './meeting-room/entities/meeting-room.entity';
import { BookingModule } from './booking/booking.module';
import { Booking } from './booking/entities/booking.entity';
import { StatisticModule } from './statistic/statistic.module';
import * as path from 'path';
@Module({
  imports: [
    JwtModule.registerAsync({
      global:true,
      inject:[ConfigService],
      useFactory(configServer: ConfigService){
        return {
          secret: configServer.get('jwt_secret'),
          signOptions: {
            expiresIn: configServer.get('jwt_access_token_expires_time') //默认30m
          }
        }
      }
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      // envFilePath: '.env',
      envFilePath: path.join(__dirname, "../.env")
    }),
    TypeOrmModule.forRootAsync({
      useFactory(configServer: ConfigService){
        return {
          type: 'mysql',
          host: configServer.get('mysql_server_host'),
          port: configServer.get('mysql_server_port'),
          username: configServer.get('mysql_server_username'),
          password: configServer.get('mysql_server_password'),
          database: configServer.get('mysql_server_database'),
          entities: [User, Role, Permission, MeetingRoom, Booking],
          synchronize: false,
          logging: false,
          poolSize: 10,
          connectorPackage: 'mysql2',
          extra:{
            authPlugins: 'caching_sha2_password'
          }
        }
      },
      inject:[ConfigService]
    }),
    UserModule,
    RedisModule,
    EmailModule,
    MeetingRoomModule,
    BookingModule,
    StatisticModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'APP_GUARD',
      useClass: LoginGuard
    },
    {
      provide: 'APP_GUARD',
      useClass: PermissionGuard
    }
  ],
})
export class AppModule {}
